package com.rattsoftware.client.controllers;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.rattsoftware.client.AppMessages;
import com.rattsoftware.client.RefreshableEvent;
import com.rattsoftware.client.RefreshablePublisher;
import com.rattsoftware.client.webclient;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.DepartmentSummary;
import com.rattsoftware.client.model.FacultySummary;
import com.rattsoftware.client.model.TestData;
import com.rattsoftware.client.util.ProgressIndicator;
import com.rattsoftware.client.views.DepartmentView;
import com.rattsoftware.server.admin.model.CourseUnit;
import com.rattsoftware.server.admin.model.Department;
import com.rattsoftware.server.admin.model.Editable;
import com.rattsoftware.server.admin.model.exceptions.RattsParsingException;

public class DepartmentController extends Controller{

	AppMessages appMessages = GWT.create(AppMessages.class);

	private DepartmentView departmentView;
	public DepartmentController() {
		super();
		registerEventTypes(AppEvents.ViewDepartments);
	}
	
	@Override
	protected void initialize() {
		GWT.log("DepartmentController : initialize");
		departmentView = new DepartmentView(this);
		RefreshablePublisher.get().subscribe(RefreshableEvent.Type.UPDATE_COURSE_UNIT, departmentView);
		Registry.get(webclient.DEPARTMENT_SERVICE);
	}

	@Override
	public void handleEvent(AppEvent event) {
		GWT.log("DepartmentController : handleEvent");
		EventType type = event.getType();
		if (type == AppEvents.ViewDepartments) {
			forwardToView(departmentView, event);
		}
	}
	
	public void getDepartments(final PagingLoadConfig pagingLoadConfig, 
			final AsyncCallback<com.extjs.gxt.ui.client.data.PagingLoadResult<DepartmentSummary>> callback)
			throws RattsParsingException {
		GWT.log("DepartmentController : getDepartments");
		
		/*courseService.getCourseList(user, PagingUtil.createPagingLoadConfig(pagingLoadConfig),
				new RattsAsyncCallback<PagingLoadResult<Course>>() {
					@Override
					public void onSuccess(PagingLoadResult<Course> result) {*/
						ProgressIndicator.hideProgressBar();
						List<DepartmentSummary> results = new ArrayList<DepartmentSummary>();
						//List<Course> data = result.getData();
						List<Department> data = TestData.getDepartments();
						
						for (Department f : data) {
							results.add(new DepartmentSummary(f));
						}
						
						departmentView.setAllDepartmentSummaries(results); // reset the list
						
						callback.onSuccess(new BasePagingLoadResult<DepartmentSummary>(results, 0, 20));
						/*callback.onSuccess(new BasePagingLoadResult<CourseSummary>(results, result.getOffset(), result
								.getTotalLength()));
					}
				});*/
	}
	
	public void deleteDepartment(final Editable editable) {
		if (editable instanceof CourseUnit) {

			GWT.log("DepartmentController : delete Department");
			/*courseUnitService.deleteCourseUnit((CourseUnit)editable, new RattsAsyncCallback<Void>() {
				@Override
				public void onSuccess(Void result) {*/
					RefreshablePublisher.get().publish(new RefreshableEvent(RefreshableEvent.Type.DELETE_DEPARTMENT, editable));
				/*}
			});*/
		}
	}
	
	public void forwardToEditDepartmentController(DepartmentSummary departmentSummary, FacultySummary facultySummary) {
		GWT.log("DepartmentController : forwardToEditDepartmentController");
		Dispatcher dispatcher = Dispatcher.get();
		com.extjs.gxt.ui.client.mvc.AppEvent editDepartmentEvent = new com.extjs.gxt.ui.client.mvc.AppEvent(AppEvents.EditDepartment);
		editDepartmentEvent.setData("department", departmentSummary);
		editDepartmentEvent.setData("faculty", facultySummary);
		
		dispatcher.dispatch(editDepartmentEvent);
	}
}
